/*
 * 1.00 2012/01/31
 *
 * Copyright (c) 2012 Motch_bs
 * All rights reserved.
 *
 * このソフトウェアは，Motch_bsに所有される機密情報である。
 * これらの機密情報を漏洩してはならず，Motch_bs の意図する
 * 認可の元においてこれを使用すべし。
 */
package jp.co.ufp.sevendaystask_free.storage;

import jp.co.ufp.sevendaystask_free.constants.C;
import jp.co.ufp.sevendaystask_free.logics.Logic001;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * データベースヘルパークラス
 * 
 * @author Motch_bs
 * 
 */
public class QueryManager extends SQLiteOpenHelper {

    /** コンテキスト */
    private Context context;

    /** デフォルトの分 */
    private static final String TIME_MINUTE = "00";

    /** テーブル定義 */
    public static final String CREATE_TBL_7DAYTASK = "CREATE TABLE "
	    + C.TABLE + "( "
	    + C.TASKID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
	    + C.WEEKDAY + " TEXT,"
	    + C.TIME_START + " TEXT,"
	    + C.TIME_END + " TEXT,"
	    + C.TASK_TITLE + " TEXT,"
	    + C.TASK_MEMO + " TEXT,"
	    + C.TASK_COLOR + " INTEGER,"
	    + C.TASK_FONT + " TEXT,"
	    + C.TASK_FONT_SIZE + " TEXT DEFAULT '20',"
	    + C.TASK_NOTIFY_FLG + " INTEGER,"
	    + C.TASK_PLACE + " TEXT,"
	    + C.TASK_ICON + " TEXT,"
	    + C.TASK_IMAGE + " TEXT,"
	    // GPS
	    + C.GPS_LATITUDE + " TEXT,"
	    + C.GPS_LONGITUDE + " TEXT,"
	    + C.GPS_ACCURACY + " TEXT,"
	    + C.GPS_ALTITUDE + " TEXT,"
	    + C.GPS_TIME + " TEXT,"
	    + C.GPS_SPEED + " TEXT,"
	    + C.GPS_BEARING + " TEXT"
	    + ");";

    /**
     * @param context
     */
    public QueryManager(Context pContext) {
	super(pContext, C.DB_NAME, null, C.DB_VERSION);
	this.context = pContext;
    }

    /**
     * @see android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite
     *      .SQLiteDatabase)
     */
    @Override
    public void onCreate(
	SQLiteDatabase db) {
	// データベーステーブルの作成
	db.execSQL(CREATE_TBL_7DAYTASK);
	Log.i("QueryManager", "installed database.");
	// 初期値の挿入
	String[] defaultData = Logic001.getDefaultData(context);
	int startTime = 7;
	for (int i = 0; i < 7; i++) {
	    for (int j = 0; j < defaultData.length; j++) {
		ContentValues cv = new ContentValues();
		// 初期時刻
		String timeStart = String.format("%02d", startTime + (j * 2)) + TIME_MINUTE;
		String timeEnd = String.format("%02d", startTime + ((j * 2) + 2)) + TIME_MINUTE;
		// 登録処理
		cv.put(C.WEEKDAY, Integer.valueOf(i));
		cv.put(C.TIME_START, timeStart);
		cv.put(C.TIME_END, timeEnd);
		cv.put(C.TASK_TITLE, defaultData[j]);
		cv.put(C.TASK_MEMO, "");
		cv.put(C.TASK_COLOR, C.DEFAULT_COLORS[j]);
		db.insert(C.TABLE, null, cv);
	    }
	}
    }

    /**
     * @see android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite
     *      .SQLiteDatabase, int, int)
     */
    @Override
    public void onUpgrade(
	SQLiteDatabase db, int oldVersion, int newVersion) {
	// データベースの再作成
	db.execSQL("DROP TABLE IF EXISTS" + C.TABLE);
	onCreate(db);

    }

}
